﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.bill
{
    public partial class InMoneyList : BaseAdmin
    {
        readonly dalCompany theCompany = new dalCompany();
        readonly DALInMoney theInMoney = new DALInMoney();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.InMoneyList);
                this.BindCompanyList();
                this.BindGridView();
            }
        }

        void BindCompanyList()
        {
            DataSet ds = theCompany.GetList("");
            this.ddlCompanyList.DataTextField = "name";
            this.ddlCompanyList.DataValueField = "id";
            this.ddlCompanyList.DataSource = ds;
            this.ddlCompanyList.DataBind();
            this.ddlCompanyList.Items.Insert(0, new ListItem("请选择公司名称...", ""));

            if (base.CompanyId != null)
            {
                this.ddlCompanyList.Items.FindByValue(base.CompanyId).Selected = true;
                this.ddlCompanyList.Enabled = false;
            }
        }


        void BindGridView()
        {
            DataSet ds;
            StringBuilder sbWhere = new StringBuilder();
            sbWhere.Append("1=1");

            if (this.ddlCompanyList.SelectedItem.Value != "")
            {
                sbWhere.AppendFormat(" and CompanyId={0}", this.ddlCompanyList.SelectedItem.Value);
            }
            if (!string.IsNullOrEmpty(this.txtSartTime.Value.Trim()))
            {
                sbWhere.AppendFormat(" and AddTime >= convert(datetime,'{0}')", this.txtSartTime.Value);
            }

            if (!string.IsNullOrEmpty(this.txtEndTime.Value.Trim()))
            {
                sbWhere.AppendFormat(" and convert(datetime,convert(varchar(10),AddTime,121)) <= convert(datetime, '{0}')", this.txtEndTime.Value);
            }

            ds = theInMoney.GetList(sbWhere.ToString());
            if (ds.Tables.Count > 0)
            {
                this.GridView1.DataSource = ds.Tables[0].DefaultView;
                this.GridView1.DataBind();

                this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
                this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
                this.LblRecordCount.Text = "总计 " + ds.Tables[0].Rows.Count.ToString() + "个记录";
                if (ds.Tables[0].Rows.Count == 0)
                {
                    this.btnFirst.Visible = false;
                    this.btnPrev.Visible = false;
                    this.btnNext.Visible = false;
                    this.btnLast.Visible = false;
                    this.LblCurrentIndex.Visible = false;
                    this.LblPageCount.Visible = false;
                    this.LblRecordCount.Visible = false;
                }
                else if (this.GridView1.PageCount == 1)
                {
                    this.btnFirst.Visible = false;
                    this.btnPrev.Visible = false;
                    this.btnNext.Visible = false;
                    this.btnLast.Visible = false;
                    this.LblCurrentIndex.Visible = true;
                    this.LblPageCount.Visible = true;
                    this.LblRecordCount.Visible = true;
                }
                else
                {

                    this.btnFirst.Visible = true;
                    this.btnPrev.Visible = true;
                    this.btnNext.Visible = true;
                    this.btnLast.Visible = true;
                    this.LblCurrentIndex.Visible = true;
                    this.LblPageCount.Visible = true;
                    this.LblRecordCount.Visible = true;
                }
                this.btnFirst.CommandName = "1";
                this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
                this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
                this.btnLast.CommandName = this.GridView1.PageCount.ToString();
                this.ddlCurrentPage.Items.Clear();
                for (int i = 1; i <= this.GridView1.PageCount; i++)
                {
                    this.ddlCurrentPage.Items.Add(i.ToString());
                }
                if (ddlCurrentPage.Items.Count > 0)
                {
                    this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
                }
            }
        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.BindGridView();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.BindGridView();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            {

                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
                    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#efefef\"");
                }

            }
        }

        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            this.BindGridView();
        }

    }



}